package com.jilian.mydemo.modul.main.ui;

import android.Manifest;
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.jilian.mydemo.Constant;
import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.BannerDto;
import com.jilian.mydemo.common.dto.BannerListDto;
import com.jilian.mydemo.common.vo.BannerVo;
import com.jilian.mydemo.factory.Factoty;
import com.jilian.mydemo.modul.main.viewmodel.MainViewModel;
import com.jilian.mydemo.modul.user.activity.LoginActivity;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.PermissionsObserver;
import com.jilian.mydemo.utils.SPUtil;
import com.jilian.mydemo.utils.ToastUitl;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.ArrayList;
import java.util.List;

public class WelcomeActivity extends FragmentActivity {
    private UserViewModel viewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
        RxPermissions.getInstance(this).request(
                Manifest.permission.RECORD_AUDIO,//录音权限
                Manifest.permission.ACCESS_COARSE_LOCATION, //定位权限
                Manifest.permission.ACCESS_FINE_LOCATION,//定位权限
                Manifest.permission.WRITE_EXTERNAL_STORAGE,//写权限
                Manifest.permission.READ_EXTERNAL_STORAGE, //读权限
                Manifest.permission.CAMERA,//相机权限
                Manifest.permission.CALL_PHONE,//拨
                Manifest.permission.READ_PHONE_STATE//

        ).subscribe(new PermissionsObserver() {
            @Override
            protected void onGetPermissionsSuccess() {
                WelcomeActivity.this.goToMain();
            }

            @Override
            protected void onGetPermissionsSuccessFailuer() {
                finish();
                ToastUitl.showImageToastTips("获取权限失败");
            }
        });

    }

    /**
     *
     */
    private void goToMain() {

        //
        //判断有没有进入引导页
        boolean isComin = SPUtil.getData(Constant.SP_VALUE.CONSTANT, Constant.SP_VALUE.ISCOMIN, Boolean.class, false);
        //实名认证卡口，根据返回状态进行跳转
        if (isComin) {
            //判断 session是否存在
            String cookieStr = SPUtil.getData(Constant.SP_VALUE.SP, Constant.SP_VALUE.SESSION_ID, String.class, null);
            //如果 session 不存在 则需要登录
            if (TextUtils.isEmpty(cookieStr) || !cookieStr.contains("SHUIGONG_JSESSIONID")) {
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
            }
            //已经登录
            else {
                check();
            }

        } else {
            //没有进入引导页，则获取引导页
            getBanner();
        }


    }

    private List<BannerDto> list;

    /**
     * 获取引导页
     */
    private void getBanner() {
        BannerVo vo = new BannerVo();
        vo.setLocation(2);
        viewModel.getAppAdList(vo);
        viewModel.getBannerLiveData().observe(this, new Observer<BaseDto<BannerListDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<BannerListDto> dto) {
                if (dto.isSuccess() && dto.getData() != null && EmptyUtils.isNotEmpty(dto.getData().getList())) {
                    list = new ArrayList<>();
                    list.addAll(dto.getData().getList());
                    Intent intent = new Intent(WelcomeActivity.this, SplashActivity.class);
                    intent.putExtra("list", JSONObject.toJSONString(list));
                    startActivity(intent);

                } else {
                    //如果没有 引导页   去登录
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                }
            }
        });
    }

    /**
     * 检查审核状态
     */
    private void check() {
        //  1 已注册 2 测验通过 3 实名通过 4 已认证
        // 1 跳到 开始考试 2 跳到 实名验证 3 跳到正在审核 4 跳到审核成功 5 跳到审核失败
        viewModel.check();
        viewModel.getCheckliveData().observe(this, new Observer<BaseDto<Integer>>() {
            @Override
            public void onChanged(@Nullable BaseDto<Integer> integerBaseDto) {
                if (integerBaseDto.getCode() == Constant.Server.SUCCESS_CODE) {
                    if (integerBaseDto.getData() != null) {
                        if (integerBaseDto.getData() == 1) {
                            startToLogin();
                        } else if (integerBaseDto.getData() == 2) {
                            startToLogin();

                        } else if (integerBaseDto.getData() == 3) {
                            startToLogin();
                        } else if (integerBaseDto.getData() == 4) {
                            startToLogin();
                        } else if (integerBaseDto.getData() == 5) {
                            startToLogin();
                        } else {
                            startActivity(new Intent(WelcomeActivity.this, MainActivity.class));
                        }

                    } else {
                        ToastUitl.showImageToastTips(integerBaseDto.getMsg());
                    }
                } else {
                    ToastUitl.showImageToastTips(integerBaseDto.getMsg());
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                }
            }
        });
    }

    private void startToLogin() {
        SPUtil.clearData(Constant.SP_VALUE.SP);
        startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
        finish();
        MyApplication.clearAllActivitys();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }
}
